package service;

import dao.UserDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.User;

import java.util.List;

@Service
public class UserService {
    @Autowired
    UserDao userdao;

    /**
     * 查询业务
     * @param phone
     * @return
     */
    public User queryByPhone(String phone){
        return userdao.queryByPhone(phone);
    }


    /**
     * 获取所有用户
     * @return
     */
    public List<User> queryAllUser(){
        return userdao.queryAllUser();
    }




    /**
     * 登录业务
     * @param phone
     * @param password
     * @return null表示账号密码错误；否则为成功
     */
    public User login(String phone, String password){
        if(userdao.queryByPhone(phone) == null){
            return null;
        }
        User user =  userdao.login(phone,password);
        return user;
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    public int addUser(User user){
        //避免插入的用户手机号已经存在
        if(queryByPhone(user.getPhone())!=null){
            return 0;
        }
        return userdao.insertUser(user);
    }


    /**
     * 更新用户
     * @param user
     * @return
     */
    public int updateUser(User user,String oldPhone){
        //手机号不更改或手机号不存在则执行更新
        if(oldPhone.equals(user.getPhone()) || queryByPhone(user.getPhone()) == null){
            return userdao.updateUser(user);
        }
        return 0;
    }

}
